package com.jss.jssmybatisplus.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.yulichang.injector.MPJSqlInjector;
import com.github.yulichang.interceptor.MPJInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author Shuaishuai.Ji
 * @Date 2022/8/22 16:12
 * @Version 1.0
 */
@Configuration
@EnableTransactionManagement  // 开启事务
public class MybatisPlusConfig {
//    @Primary
//    @Bean("db1SqlSessionFactory")
//    public SqlSessionFactory db1SqlSessionFactory(DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
//        sqlSessionFactory.setDataSource(dataSource);
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        // 这里设置不适用缓存
//        sqlSessionFactory.setConfiguration(configuration);
//        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
//        // 启动不打印log
//        sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
//        // 分页插件PaginationInnerInterceptor
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
////        interceptor.addInnerInterceptor(new MPJInterceptor());
//        sqlSessionFactory.setPlugins(interceptor);
//        return sqlSessionFactory.getObject();
//    }

    /**
     * mybatis plus 插件(以分页插件为例)
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //其他拦截器插件配置 略
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

//    @Bean
//    public ISqlInjector sqlInjector() {
//        return new MPJSqlInjector() {
//            @Override
//            public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
//                List<AbstractMethod> list = super.getMethodList(mapperClass);
//                //添加你的方法
//                list.add(new InsertBatchSomeColumn());
//                return list;
//            }
//        };
//    }
    @Bean
    public MPJSqlInjector mpjSqlInjector() {
        return new MPJSqlInjector();
    }

    /**
     * GlobalConfig 配置 MPJSqlInjector
     * 其他配置 略
     */
    @Bean
    public GlobalConfig globalConfig(MPJSqlInjector mpjSqlInjector) {
        GlobalConfig config = new GlobalConfig();
        config.setSqlInjector(mpjSqlInjector);
        return config;
    }

    /**
     * 关联SqlSessionFactory与GlobalConfig
     * 设置mybatis 拦截器
     * 其他配置 略
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig,
                                               MybatisPlusInterceptor mybatisPlusInterceptor, MPJInterceptor mpjInterceptor) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        //关联SqlSessionFactory与GlobalConfig
        sessionFactory.setGlobalConfig(globalConfig);
        //设置mybatis 拦截器 (注意顺序 mpjInterceptor要在最后)
        sessionFactory.setPlugins(mybatisPlusInterceptor, mpjInterceptor);
        //其他配置 略
        return sessionFactory.getObject();
    }





    //
    //    @Bean
    //    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
    //        // 序列化枚举值为数据库存储值
    //        FastJsonConfig config = new FastJsonConfig();
    //        config.setSerializerFeatures(SerializerFeature.WriteEnumUsingToString);
    //
    //        return properties -> {
    //            GlobalConfig globalConfig = properties.getGlobalConfig();
    //            globalConfig.setBanner(false);
    //            MybatisConfiguration configuration = new MybatisConfiguration();
    //            configuration.setDefaultEnumTypeHandler(MybatisEnumTypeHandler.class);
    //            properties.setConfiguration(configuration);
    //        };
    //    }
}


